package com.flowable.controller;

import com.flowable.entity.TaskApprovalDTO;
import com.flowable.entity.TaskDTO;
import com.flowable.service.FlowableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Tag(name = "flowable流程学习")
@Slf4j
@RestController
@RequestMapping("/flowable")
public class FlowableController {

    /** 流程变量 - 请假人姓名 */
    private static final String VAR_LEAVE_NAME = "name";
    /** 流程变量 - 请假原因 */
    private static final String VAR_LEAVE_REASON = "reason";
    /** 流程变量 - 请假天数 */
    private static final String VAR_LEAVE_DAYS = "days";
    /** 流程变量 - 重试次数 */
    private static final String VAR_RETRY_COUNT = "retryCount";

    private final TaskService taskService;
    private final RuntimeService runtimeService;
    private final FlowableService flowableService;

    @Autowired
    public FlowableController(TaskService taskService, RuntimeService runtimeService, FlowableService flowableService) {
        this.taskService = taskService;
        this.runtimeService = runtimeService;
        this.flowableService = flowableService;
    }

    /**
     * 创建flowable流程
     * @param taskDTO 创建流程
     * @return 返回状态
     */
    @Operation(summary = "创建流程")
    @PostMapping("/create")
    public String createFlowable(
           @RequestBody TaskDTO taskDTO
    ) {
        return flowableService.createFlowable(taskDTO);
    }

    @Operation(summary = "组长审批")
    @PutMapping("/LeaderAgree")
    public String agreeLeaderFlowable(@RequestBody TaskApprovalDTO taskApprovalDTO){
        return flowableService.agreeLeaderFlowable(taskApprovalDTO);
    }

    @Operation(summary = "经理审批")
    @PutMapping("/mangerAgree")
    public String agreeMangerFlowable(@RequestBody TaskApprovalDTO taskApprovalDTO){
        return flowableService.agreeMangerFlowable(taskApprovalDTO);
    }


    /**
     * 获取流程信息
     * @param assignee 用户名
     * @return 返回流程信息
     */
    @Operation(summary = "获取流程信息")
    @GetMapping
    public List<Map<String, Object>> getTasksByAssignee(
            @RequestParam("assignee") String assignee
    ){
        return flowableService.getTasksByAssignee(assignee);
    }



}
